<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<script type="text/javascript">
		debugger;
			$(function(){
				$('#menuTreeGridTable').treegrid({
					title:'菜单管理',
					iconCls:'icon-save',
					//width:700,
					//height:350,
					nowrap: false,
					rownumbers: true,
					animate:true,
					collapsible:true,
					url:'${pageContext.request.contextPath}/sys/menu.do?queryList',
					idField:'id',
					treeField:'functionname',
					frozenColumns:[[
		                {title:'菜单树',field:'functionname',width:200,
			                formatter:function(value){
			                	return '<span style="color:red">'+value+'</span>';
			                }
		                }
					]],
					columns:[[

						{field:'url',title:'访问地址',width:250,rowspan:4,formatter:function(value,row,index){
							return row.url;
							}
						},
						{field:'sortid',title:'排序',width:100,formatter:function(value,row,index){
							return row.sortid;
							}
						},
						//{field:'isbottom',title:'是否底级',width:150,formatter:function(value,row,index){
						//	return row.isbottom;
						//	}
						//},
						{field:'isbottomshow',title:'是否底级',width:150,formatter:function(value,row,index){
							return (row.isbottom=="1") ? '是':'否';
							}
						},
						//{field:'isdisabled',title:'是否禁用',width:150,formatter:function(value,row,index){
						//	return row.isdisabled;
						//	}
						//},
						{field:'isdisabledshow',title:'是否禁用',width:150,formatter:function(value,row,index){
							return (row.isdisabled=="1") ? '是':'否';
							}
						}
					]],
					
					onContextMenu: function(e,row){
						e.preventDefault();
						$(this).treegrid('unselectAll');
						$(this).treegrid('select', row.id);
						$('#menuTreeGridRightMenu').menu('show', {
							left: e.pageX,
							top: e.pageY
						});
					}
				});
			});
			
			function appendMenu(){
				debugger;
				var node = $('#menuTreeGridTable').treegrid('getSelected');//获取 当前 选中的 节点
				if(""==node){
					$.messager.alert('提示',"请选择您要添加的节点",'info');
					return;
				}
				
				var initData = {fatherid:node.id,fathername:node.functionname};
				showWindow({
		  			title:'增加菜单',
		  			href:'${pageContext.request.contextPath}/sys/menu.do?popWindow',
		  			width:400,
		  			height:300,
		  			onLoad: function(){
		  				//$('#menuForm').form('clear');
		  				$('#menuForm').form('load',initData);//初始化数据
		  			}
		  			
		  		});
			}
			
			//删除
			function removeMenu(){
				var node = $('#menuTreeGridTable').treegrid('getSelected');//获取 当前 选中的 节点
				if(""==node){
					$.messager.alert('提示',"请选择您要添加的节点",'info');
					return;
				}
				if(node.id == 0){
					$.messager.alert('提示',"该节点是根节点，不能删除",'info');
					return;
				}
				var childrennode = $('#menuTreeGridTable').treegrid('getChildren', node.functionname);
				if(""!=childrennode){
					$.messager.alert('提示',"该节点有子节点，不能删除",'info');
					return;
				}
				
				$.messager.confirm('提示','确定要删除吗?',function(result){
			        if (result){
			        	var ps = "";
			        	ps += "&id="+node.id;
			        	
			        	$.post('${pageContext.request.contextPath}/sys/menu.do?delete'+ps,function(data){
				        	$('#menuTreeGridTable').treegrid('reload'); 
			        		$.messager.alert('提示',data.msg,'info');
			        	});
			        }
			    });
			}
			
			//编辑
			function editMenu(){
			debugger;
				var node = $('#menuTreeGridTable').treegrid('getSelected');//获取 当前 选中的 节点
				if(""==node){
					$.messager.alert('提示',"请选择您要添加的节点",'info');
					return;
				}
				if(node.id == 0){
					$.messager.alert('提示',"该节点是根节点，不能编辑",'info');
					return;
				}
				//var initData = {parentid:node[0].id,parentname:node[0].functionname};
				var parentnameNode = $('#menuTreeGridTable').treegrid('getParent',node.id);
				if("" != parentnameNode)
					node.fathername=parentnameNode.functionname;
				showWindow({
		  			title:'编辑菜单',
		  			href:'${pageContext.request.contextPath}/sys/menu.do?popWindow',
		  			width:400,
		  			height:300,
		  			onLoad: function(){
		  				$('#menuForm').form('load',node);//初始化数据
		  			}
		  			
		  		});
			}
			
			//禁用或者启用
			function unabledOrInabledMenu(){
				var node = $('#menuTreeGridTable').treegrid('getSelected');//获取 当前 选中的 节点
				if(""==node){
					$.messager.alert('提示',"请选择您要添加的节点",'info');
					return;
				}
				if(node.id == 0){
					$.messager.alert('提示',"该节点是根节点，不能禁用",'info');
					return;
				}
				var ps = "";
	        	ps += "&id="+node.id;
	        	
	        	$.post('${pageContext.request.contextPath}/sys/menu.do?unabledOrInabled'+ps,function(data){
		        	$('#menuTreeGridTable').treegrid('reload'); 
	        		$.messager.alert('提示',data.msg,'info');
	        	});
				
			}
		</script>
	</head>
	<body >
		<table id="menuTreeGridTable" ></table>
	
		<div id="menuTreeGridRightMenu" class="easyui-menu" style="width:120px;">
			<div onclick="appendMenu()">添加</div>
			<div onclick="editMenu()">修改</div>
			<div onclick="unabledOrInabledMenu()">禁用|启用</div>
			<div onclick="removeMenu()">删除</div>
		</div>
	</body>
</html>